﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.Car_Rental;
using Entity.Car_Rental;
using System.Text;

namespace Car_Rental.Commons.Controllers.Helpers
{
    public static class ViewHelper
    {
        #region 地址级联选择控件，省：Province、市：City、县：District

        public static MvcHtmlString AddressSelect(this HtmlHelper html)
        {
            return AddressSelect(html, null);
        }

        /// <summary>
        /// 地址级联选择控件，省：Province、市：City、县：District
        /// </summary>
        /// <param name="html"></param>
        /// <param name="district">县名称</param>
        /// <returns></returns>
        public static MvcHtmlString AddressSelect(this HtmlHelper html, string district)
        {
            string jsUrl = "/Scripts/address.js";
            IAreaRepository iAreaRepository = new AreaRepository();
            List<Area> ProvinceData = iAreaRepository.GetChildrenAddressByCodeID(0);
            StringBuilder Control = new StringBuilder();
            if (string.IsNullOrEmpty(district))
            {
                Control.Append("<select id=\"Province\" name=\"Province\">");
                Control.Append("<option value=\"\">请选择省份</option>");
                foreach (Area item in ProvinceData)
                {
                    Control.Append("<option value=\"" + item.Name + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");
                Control.Append("<select id=\"City\" name=\"City\">");
                Control.Append("<option value=\"\">请选择城市</option>");
                Control.Append("</select>");
                Control.Append("<select id=\"District\" name=\"District\">");
                Control.Append("<option value=\"\">请选择区/县</option>");
                Control.Append("</select>");
            }
            else
            {
                //选中的县
                Area District = iAreaRepository.GetAddressByName(district, 6);
                //县列表
                List<Area> DistrictData = iAreaRepository.GetChildrenAddressByCodeID(District.ParentID);
                //选中的市
                Area City = iAreaRepository.GetAddressByCodeID(District.ParentID);
                //市列表
                List<Area> CityData = iAreaRepository.GetChildrenAddressByCodeID(City.ParentID);
                //选中的省
                Area Province = iAreaRepository.GetAddressByCodeID(City.ParentID);
                Control.Append("<select id=\"Province\" name=\"Province\">");
                Control.Append("<option value=\"\">请选择省份</option>");
                foreach (Area item in ProvinceData)
                {
                    if (item.CodeID == Province.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.Name + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.Name + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");
                Control.Append("<select id=\"City\" name=\"City\">");
                Control.Append("<option value=\"\">请选择城市</option>");
                foreach (Area item in CityData)
                {
                    if (item.CodeID == City.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.Name + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.Name + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");
                Control.Append("<select id=\"District\" name=\"District\">");
                Control.Append("<option value=\"\">请选择区/县</option>");
                foreach (Area item in DistrictData)
                {
                    if (item.CodeID == District.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.Name + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.Name + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");
            }
            Control.Append("<script type=\"text/javascript\" src=\"" + jsUrl + "\"></script>");
            return MvcHtmlString.Create(Control.ToString());
        }

        /// <summary>
        /// 地址列表框，以ID为值
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString AddressSelectByID(this HtmlHelper html)
        {
            return AddressSelectByID(html, -1);
        }
        /// <summary>
        /// 地址列表框，以ID为值
        /// </summary>
        /// <param name="html"></param>
        /// <param name="district">当前选中的值</param>
        /// <returns></returns>
        public static MvcHtmlString AddressSelectByID(this HtmlHelper html, int district)
        {
            string jsUrl = "/Scripts/addressByID.js";
            IAreaRepository iAreaRepository = new AreaRepository();
            List<Area> ProvinceData = iAreaRepository.GetChildrenAddressByCodeID(0);
            StringBuilder Control = new StringBuilder();
            if (district == 0)
            {
                Control.Append("<select id=\"Province\" name=\"Province\">");
                Control.Append("<option value=\"-1\">请选择省份</option>");
                foreach (Area item in ProvinceData)
                {
                    Control.Append("<option value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");
                Control.Append("<select id=\"City\" name=\"City\">");
                Control.Append("<option value=\"-1\">请选择城市</option>");
                Control.Append("</select>");
                Control.Append("<select id=\"District\" name=\"District\">");
                Control.Append("<option value=\"-1\">请选择区/县</option>");
                Control.Append("</select>");
            }
            else
            {
                //选中的县
                Area District = iAreaRepository.GetModel().GetAddressByCodeLength(6).GetAddressByCodeID(district);
                Area City = new Area();
                Area Province = new Area();
                List<Area> CityData = new List<Area>();
                List<Area> DistrictData = new List<Area>();

                if (District != null)
                {
                    //县列表
                    DistrictData = iAreaRepository.GetChildrenAddressByCodeID(District.ParentID);
                    //选中的市
                    City = iAreaRepository.GetModel().GetAddressByCodeLength(4).GetAddressByCodeID(District.ParentID);
                    //市列表
                    CityData = iAreaRepository.GetChildrenAddressByCodeID(City.ParentID);
                    //选中的省
                    Province = iAreaRepository.GetModel().GetAddressByCodeLength(2).GetAddressByCodeID(City.ParentID);
                }
                else
                {
                    City = iAreaRepository.GetModel().GetAddressByCodeLength(4).GetAddressByCodeID(district);
                    if (City != null)
                    {
                        CityData = iAreaRepository.GetChildrenAddressByCodeID(City.ParentID);
                    }
                    else
                        Province = iAreaRepository.GetModel().GetAddressByCodeLength(2).GetAddressByCodeID(district);
                }

                Control.Append("<select id=\"Province\" name=\"Province\">");
                Control.Append("<option value=\"-1\">请选择省份</option>");
                foreach (Area item in ProvinceData)
                {
                    if (Province != null && item.CodeID == Province.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");

                Control.Append("<select id=\"City\" name=\"City\">");
                Control.Append("<option value=\"-1\">请选择城市</option>");
                foreach (Area item in CityData)
                {
                    if (City != null && item.CodeID == City.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");

                Control.Append("<select id=\"District\" name=\"District\">");
                Control.Append("<option value=\"-1\">请选择区/县</option>");
                foreach (Area item in DistrictData)
                {
                    if (District != null && item.CodeID == District.CodeID)
                        Control.Append("<option selected=\"selected\" value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                    else
                        Control.Append("<option value=\"" + item.CodeID + "\">" + item.Name + "</option>");
                }
                Control.Append("</select>");

            }
            Control.Append("<script type=\"text/javascript\" src=\"" + jsUrl + "\"></script>");
            return MvcHtmlString.Create(Control.ToString());
        }

        #endregion
    }
}